const path = require("path")
const fs = require("fs-extra")
function getPage(name) {
	const pagePath = path.resolve(__dirname, "../src", `./${name}`)
	function getFile(scanPath, basePath) {
		return fs.readdirSync(scanPath).map(m => {
			const filePath = path.resolve(scanPath, m)
			const checkResult = fs.statSync(filePath)
			if (checkResult.isDirectory()) {
				return getFile(filePath, basePath + "/" + m)
			} else {
				return basePath + "/" + m
			}
		})
	}
	return getFile(pagePath, name)
}

function run() {
	const routes = getPage("pages")
		.flat(Infinity)
		.filter(m => m.includes("tsx"))
		.map(m => m.split(".").shift())
		.sort((next, prev) => prev.indexOf("index/index"))
	const target = path.resolve(__dirname, "../", `./routes.js`)
	fs.writeFileSync(target, `module.exports=${JSON.stringify(routes)}`, { encoding: "utf-8" })
}
run()
